package com.panels.cell;

import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

public class ValueComboBox extends JComboBox{
	
    

	public ValueComboBox(Vector data,boolean withVoid) {
		
		
		super(addVoid(data,withVoid));
		
	}

	public ValueComboBox() {
		super();
	}

	private static Vector addVoid(Vector data,boolean withVoid) {
		
		
		ValuePair record=new ValuePair("","");
		
		Vector newData=new Vector();
			
		if(withVoid)
			newData.add(0,record);
		
		int size=data.size();
		
		for (int i = 0; i < size; i++) {
			newData.add(data.elementAt(i));
		}
		
		return newData;
	}

	public String getSelectedCode(){
		
		if(getSelectedIndex()<0)
			return null;
		
	
		ValuePair obj=(ValuePair) getSelectedItem();
		
		
		if(obj==null || obj.getCode().equals(""))
			return null;
		
		String code=obj.getCode();
		return code;
		
	}
	
	
	public String getValue(){
		
		
		ValuePair obj=(ValuePair) getSelectedItem();
		
		if(obj==null)
			return null;
		return obj.getValue();
		
	}

	public void setSelectedCode(String value) { 
		

		
		if(value==null)
			return;
		
		for(int i=0;i<getModel().getSize();i++){
			
			ValuePair obj=(ValuePair)getModel().getElementAt(i);
			String code=obj.getCode();
			
			if(value.equals(code)){
			
				setSelectedIndex(i);
				break;
			}	
			
		} 
		
	}

	public void emptySelection() {
		
		setSelectedIndex(0);
		repaint();
	}
	
	

}
